import 'package:flutter/material.dart';

class BannerItem {
  final String pic; // 图片URL
  final String typeTitle; // 显示在右下角的文字
  final Color titleColor; // 文字背景颜色

  const BannerItem({
    required this.pic,
    required this.typeTitle,
    required this.titleColor,
  });

  // 可选：从JSON转换的工厂方法
  factory BannerItem.fromJson(Map<String, dynamic> json) {
    return BannerItem(
        pic: json['pic'] as String,
        typeTitle: json['typeTitle'] as String,
        titleColor: json['titleColor'] as Color);
  }

  // 可选：转换为Map的方法
  Map<String, dynamic> toJson() {
    return {
      'pic': pic,
      'typeTitle': typeTitle,
      'titleColor': titleColor,
    };
  }
}

Color parseColor(String colorStr) {
  switch (colorStr.toLowerCase()) {
    case 'red':
      return Colors.red;
    case 'blue':
      return Colors.blue;
    case 'green':
      return Colors.green;
    case 'yellow':
      return Colors.yellow;
    case 'black':
      return Colors.black;
    case 'white':
      return Colors.white;
    case 'grey':
    case 'gray':
      return Colors.grey;
    default:
      return Colors.white; // 默认颜色
  }
}

List<BannerItem> parseCarouselItems(Map<String, dynamic> json) {
  final List<dynamic> banners = json['banners'] ?? [];
  return banners.map((item) {
    return BannerItem(
      pic: item['pic'] ?? '',
      typeTitle: item['typeTitle'] ?? '',
      titleColor: parseColor(item['titleColor'] ?? ''),
    );
  }).toList();
}
